// Indexable Types 可索引的类型

interface StringArray {
  [index: number]: string; // 约束了 键名和键值
}

let myArray: StringArray;
myArray = ["Bob", "Fred"];

let myStr: string = myArray[0];


class Animal {
  name: string;
}
class Dog extends Animal {
  breed: string;
}

// Error: indexing with a numeric string might get you a completely separate type of Animal!
interface NotOkay {
  [x: number]: Animal;
  [x: string]: Dog;
}

interface NumberDictionary {
  [index: string]: number;
  length: number;    // ok, length is a number
  name: string;      // error, the type of 'name' is not a subtype of the indexer
}

interface NumberOrStringDictionary {
  [index: string]: number | string;
  length: number;    // ok, length is a number
  name: string;      // ok, name is a string
}

interface ReadonlyStringArray {
  readonly [index: number]: string;
}
let myArrays: ReadonlyStringArray = ["Alice", "Bob"];
myArrays[1] = "Mallory"; // error!


